let province = document.getElementById("province");
let city = document.getElementById("city");
// 初始化省份
cities.forEach(item => {
        let provinceName = item.provinceName;
        let option = document.createElement("option")
        option.innerHTML = provinceName;
        province.appendChild(option);
    })
    // 根据数组生成城市节点
function createCity(cityData) {
    // 清空已有数据
    let city = document.getElementById("city");
    city.innerHTML = "";
    // 根据形参生成option节点插入到城市select标签中
    cityData.forEach(item => {
        let cityName = item.cityName;
        let option = document.createElement("option")
        option.innerHTML = cityName;
        city.appendChild(option);
    })
}
// 根据数组生成区节点
function createArea(areaData) {
    let area = document.getElementById("area");
    area.innerHTML = "";
    areaData.forEach(item => {
        let option = document.createElement("option")
        option.innerHTML = item.areaName;
        area.appendChild(option)
    })
}

// 初始化城市
let cityList = cities[0].mallCityList;
createCity(cityList)

// 初始化区
let areaList = cityList[0].mallAreaList;
createArea(areaList)



province.onchange = function() {
    // selectedIndex是下拉选择菜单元素独有的一个属性
    // 返回当前选中的option标签相对于其他option的下标
    var cityList = cities[this.selectedIndex].mallCityList;
    createCity(cityList)
    createArea(cityList[0].mallAreaList)
}

city.onchange = function() {
        let areaList = cities[province.selectedIndex].mallCityList[this.selectedIndex].mallAreaList;
        createArea(areaList)
    }
    
$("#btns").click(function() {
    //姓名
    let username = $("#popname").val();
    if (username == "") {
        alert("收件人不能为空");
        return;
    }
    //电话
    let photo = $("#photo").val();
    if (photo == "") {
        alert("电话不能为空");
        return;
    }
    //邮编
    let youbian = $("#youbian").val();
    if (youbian == "") {
        alert("电话不能为空");
        return;
    }
    //三级联动
    let threecity = $("#province").val() + "-" + $("#city").val() + "-" + $("#area").val();
    //详细地址
    let hoset = $("#dizhi").val();
    if (youbian == "") {
        alert("详细地址不能为空");
        return;
    }
    //数量
    let shuliang = localStorage.getItem("onea");
    //花费
    let huafei = localStorage.getItem("oneb");
    //或这个号的姓名
    let newstuname = sessionStorage.getItem("stuname");
    $.ajax({
        method: "get",
        url: "http://127.0.0.1:80/dingdan",
        data: {
            username: username,
            photo: photo,
            youbian: youbian,
            threecity: threecity,
            hoset: hoset,
            shuliang: shuliang,
            huafei: huafei,
            newstuname: newstuname

        },
        success: function(data) {
            if (data === "success") {
                alert("添加成功");
                location.href = "http://127.0.0.1/west-point/src/jiesuan.html"
            } else {
                alert("订单信息有误");
            }
        }
    })
})